package com.ruoyi.exam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.exam.domain.ExamQuestionFavorite;
import com.ruoyi.exam.vo.ExamQuestionFavoriteVo;
import com.ruoyi.exam.vo.MyFavoriteQuestionVo;
import com.ruoyi.exam.vo.BankFavoriteQuestionInfoVo;
import com.ruoyi.exam.vo.ExamUserPracticeRecordVo;
import com.ruoyi.exam.vo.QuestionPracticeStatusVo;
import com.ruoyi.exam.mapper.ExamQuestionFavoriteMapper;
import com.ruoyi.exam.mapper.ExamUserPracticeRecordMapper;
import com.ruoyi.exam.service.IExamQuestionFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 题目收藏Service业务层处理
 *
 * @author Tellsea
 * @date 2025-05-30
 */
@Service
public class ExamQuestionFavoriteServiceImpl extends ServiceImpl<ExamQuestionFavoriteMapper, ExamQuestionFavorite> implements IExamQuestionFavoriteService {

    @Autowired
    private ExamUserPracticeRecordMapper examUserPracticeRecordMapper;

    @Override
    public TableDataInfo<ExamQuestionFavoriteVo> queryList(ExamQuestionFavoriteVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<ExamQuestionFavoriteVo> queryAll(ExamQuestionFavoriteVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public ExamQuestionFavoriteVo queryById(Long favoriteId) {
        return this.baseMapper.queryById(favoriteId);
    }

    @Override
    public TableDataInfo<MyFavoriteQuestionVo> queryMyFavoriteQuestions(Long userId, MyFavoriteQuestionVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryFavoriteQuestionsByUserId(PageUtils.buildPage(), userId, entity));
    }

    @Override
    public BankFavoriteQuestionInfoVo getBankFavoriteQuestionInfo(Long userId, Long bankId) {
        BankFavoriteQuestionInfoVo result = new BankFavoriteQuestionInfoVo();

        // 2. 获取用户在指定题库中收藏的题目列表
        List<MyFavoriteQuestionVo> favoriteQuestions = this.baseMapper.queryFavoriteQuestionsByUserIdAndBankId(userId, bankId);
        result.setFavoriteQuestions(favoriteQuestions);

        // 3. 设置收藏题目总数
        result.setTotalCount(favoriteQuestions != null ? favoriteQuestions.size() : 0);

        // 4. 获取用户在该题库的答题记录
        List<ExamUserPracticeRecordVo> practiceRecords = examUserPracticeRecordMapper.getByUserIdAndBankId(userId, bankId);

        // 5. 将答题记录转换为Map，方便查找
        Map<Long, ExamUserPracticeRecordVo> recordMap = new HashMap<>();
        if (practiceRecords != null) {
            for (ExamUserPracticeRecordVo record : practiceRecords) {
                recordMap.put(record.getQuestionId(), record);
            }
        }

        // 6. 组装题目答题状态列表，并查找未开始答的第一题
        List<QuestionPracticeStatusVo> questionList = new ArrayList<>();
        if (favoriteQuestions != null && !favoriteQuestions.isEmpty()) {
            for (int i = 0; i < favoriteQuestions.size(); i++) {
                MyFavoriteQuestionVo favoriteQuestion = favoriteQuestions.get(i);
                QuestionPracticeStatusVo statusVo = new QuestionPracticeStatusVo();
                statusVo.setQuestionId(favoriteQuestion.getQuestionId());
                statusVo.setSortOrder((long) i); // 使用收藏列表中的索引作为排序
                
                // 检查是否有答题记录
                ExamUserPracticeRecordVo record = recordMap.get(favoriteQuestion.getQuestionId());
                if (record != null) {
                    statusVo.setHasAnswered(true);
                    // "1"表示正确，"0"表示错误
                    statusVo.setIsCorrect("1".equals(record.getIsCorrect()));
                } else {
                    statusVo.setHasAnswered(false);
                    statusVo.setIsCorrect(null);
                }
                questionList.add(statusVo);
            }
            result.setFirstUnansweredQuestionId(favoriteQuestions.get(0).getQuestionId());
            result.setFirstUnansweredQuestionIndex(0);
        }
        result.setQuestionList(questionList);
        return result;
    }
}
